package com.zyh.day03

object ClojureTest {
  def main(args: Array[String]): Unit = {
    val f:()=>Unit = outer(10)
    f()

    //println(appendSuffix("test.png",".txt"))
    //println(appendSuffix("test",".txt"))
    //println(appendSuffix("test.txt",".txt"))

    //println(appendSuffix("test.txt",".png"))
    //println(appendSuffix("test1",".png"))
    //println(appendSuffix("test2.png",".png"))
    val f1:(String)=>String = makeSuffix(".txt")
    val f2:(String)=>String = makeSuffix(".png")
    println(f1("test"))
    println(f2("test"))
  }
  //返回内部函数实现外部函数的变量复用
  def makeSuffix(suffix: String): (String)=>String = {
    def isSuffix(fileName:String): String = {
      if(fileName.endsWith(suffix)) fileName else fileName+suffix
    }
    isSuffix
  }

  //判断是否以指定后缀结尾
  def appendSuffix(fileName:String,suffix:String): String = {
    if(fileName.endsWith(suffix)) fileName else fileName+suffix
  }
  //闭包
  def outer(i:Int): ()=>Unit = {
    val j:Int = 20
    def inner():Unit={
      val k = 30
      println(s"i = ${i}")
      println(s"j = ${j}")
      println(s"k = ${k}")
    }
    inner
  }
}
